import { queryDevTask, queryPlayList, queryTiMatCodes, getCityList, queryExpVehDevPlayInfo } from '../services/api';

import moment from 'moment';

function method(str, a, b) {
    let type = 'string';
    if (str === 'updateTime') {
        type = 'date';
    } else if (str === 'progId') {
        type = 'num';
    }
    switch (type) {
        case 'num': // 数字排序
            return a - b;
        case 'date': // 日期排序，IE8下'2012-12-12'这种格式无法设置时间，替换成'/'
            return moment(a).unix() - moment(b).unix();
        default:
            // 字符串排序
            return a.localeCompare(b);
    }
}
export default {
    namespace: 'playlist',

    state: {
        list: [],
        playInfo: {},
        pagination: {
            current: 1,
            pageSize: 10,
        },
        cityCodes: [],
        playScreen: [],
    },

    effects: {
        *fetch({ payload }, { call, put }) {
            const response = yield call(queryPlayList, payload);
            let { vehDevPlayInfo, ...playInfo } = response;
            let pageSize = 10;
            if (payload.pageSize) {
                pageSize = payload.pageSize * 1;
            }

            const result = {
                list: Array.isArray(vehDevPlayInfo) ? vehDevPlayInfo : [],
                playInfo,
                pagination: {
                    total: response.pageInfo.pageTotal,
                    pageSize,
                    current: parseInt(payload.page, 10) || 1,
                },
            };

            console.log('log', result);
            yield put({
                type: 'save',
                payload: result,
            });
        },
        *fetchCity({ payload }, { call, put }) {
            const response = yield call(getCityList, payload);
            const result = response.cityCodes;
            yield put({
                type: 'saveCity',
                payload: result,
            });
        },
        *fetchScreen({ payload }, { call, put }) {
            const response = yield call(queryTiMatCodes, payload);
            const result = response.tiMatCodes ? response.tiMatCodes : [];
            yield put({
                type: 'saveScreen',
                payload: result,
            });
        },
        *onExport({ payload, callback }, { call, put }) {
            const response = yield call(queryExpVehDevPlayInfo, payload);
            const rsp = response.expVehDevPlayInfo.expFileUrl;
            if (callback) callback(rsp);
        },

    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                playInfo: action.payload.playInfo,
                list: action.payload.list,
                pagination: action.payload.pagination,
            };
        },
        saveCity(state, action) {
            return {
                ...state,
                cityCodes: action.payload,
            };
        },
        saveScreen(state, action) {
            return {
                ...state,
                playScreen: action.payload,
            };
        },
    },
};
